package com.anrongtec.zcpt.utils;

import android.content.Context;

import com.anrongtec.zcpt.ApiConfig;
import com.anrongtec.zcpt.JWTApp;
import com.anrongtec.zcpt.constant.JsonDataRestult;
import com.anrongtec.zcpt.constant.URLConstants;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：liuhui on 2017/2/12 23:04
 * 邮箱：liu594545591@126.com
 * 描述：GsonUtil
 */

public class GsonUtil {
    private static Map<String, String> baseInfo;
    private static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
    private static Context context;
    // private static Map<String, Object> map;

    public static synchronized Gson getInstance() {
        if (gson == null) {
            gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        }
        return gson;
    }

    /**
     * 获取map格式公共参数
     *
     * @return
     */
	/*
	 * public static Map<String, Object> DDApp.dApp.getPublicParameter(){
	 *
	 * if (map != null) { return map; }
	 *
	 * map = new HashMap<String, Object>(); // map.put("busCode", getBusCode());
	 * map.put("appType", null); map.put("appVersion",
	 * Util.getVersionCode(context)); map.put("appOs", Util.getAppOS());
	 * map.put("deviceSN", Util.getDeviceSN(context)); map.put("deviceType",
	 * "Android"); map.put("mac", Util.getMac(context)); map.put("ip",
	 * Util.getIP(context)); map.put("longitude", MyLocation.getLongitude());
	 * map.put("latitude", MyLocation.getLatitude()); map.put("altitude",
	 * MyLocation.getAltitude()); //TODO 以下三项必须和登录时一致 map.put("imei",
	 * Util.getImei(context)); map.put("imsi", Util.getImsi(context));
	 * map.put("uuid", UUID.randomUUID().toString());
	 *
	 * return map; }
	 */

    /**
     * 解析json
     *
     * @param json
     * @param c
     * @return
     */
	/*
	 * public static <T> JsonDataRestult<T> decode(String json, Class<T> clazz){
	 * if (TextUtils.isEmpty(json)) { return null; } try { Type type = null; if
	 * (clazz.getClass() == User.class.getClass() ) { type = new
	 * TypeToken<JsonDataRestult<User>>() {}.getType(); } JsonDataRestult<T>
	 * fromJson = gson.fromJson(json, type); return fromJson;
	 *
	 * } catch (JsonSyntaxException e) { e.printStackTrace(); } return null; }
	 */

    /**
     * 解析json为type格式
     *
     * @param json
     * @param type
     *            = new TypeToken<T>() {}.getType()
     * @return
     */
    public static <T> JsonDataRestult<T> decode(String json, Type type) {
        return gson.fromJson(json, type);
    }

    /**
     * 用户登录
     */
    public static String toLoginJson(String userName, String passwd, int loginType) {
        baseInfo = JWTApp.getInstance().getBaseInfo();
        baseInfo.put(ApiConfig.BUSCODE, ApiConfig.busCode.SIGN_IN);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("baseInfo", baseInfo);
        map.put("userName", userName);
        map.put("passwd", passwd);
        map.put("loginType", loginType);
        return gson.toJson(map);
   }

    /**
     * 轮播图
     */
    public static String toBannerJson() {
        baseInfo = JWTApp.getInstance().getBaseInfo();
        baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.BANNER);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("baseInfo", baseInfo);

        return gson.toJson(map);
    }

    /**
     * 查询app
     *
     * @param wd
     *            关键字
     * @param sort
     *            排序方式(按照最新查询1、按照下载量查询2、按照好评量查询3 )
     * @param type
     *            分类查询方式：警种分类(PoliceType)、地区分类(AreaType)、功能分类(BusType)
     * @param typeVal
     *            分类查询的值
     * @param page
     *            当前查询页
     * @return
     */
    public static String toQueryJson(String wd, int sort, String type, String typeVal, int page) {
        baseInfo = JWTApp.getInstance().getBaseInfo();
        baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.QUERY);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("baseInfo", baseInfo);
        map.put("wd", wd);
        map.put("sort", sort);
        map.put("type", type);
        map.put("typeVal", typeVal);
        map.put("page", page);

        return gson.toJson(map);
    }
    /**
     * 按版本号和包名查询app
     */
    public static String toSearchApp(String pkg, int versionCode) {
        baseInfo = JWTApp.getInstance().getBaseInfo();
        baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.SEARCH_APP);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("baseInfo", baseInfo);
        map.put("pkg", pkg);
        map.put("versionCode", versionCode);

        return gson.toJson(map);
    }

    /**
     * App安装卸载日志上报
     */
    public static String toUploadJson(String type, String pkg, int versionCode) {
        baseInfo = JWTApp.getInstance().getBaseInfo();
        baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.DATA_UPLOAD);

        Map<String, Object> map = new HashMap<>();
        map.put("baseInfo", baseInfo);
        map.put("type", type);
        map.put("pkg", pkg);
        map.put("versionCode", versionCode);

        return gson.toJson(map);
    }

    /**
     * 评论查询
     */
    public static String toQueryCommentJson(String pkg, int versionCode, int page) {
        baseInfo = JWTApp.getInstance().getBaseInfo();
        baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.QUERY_COMMENT);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("baseInfo", baseInfo);
        map.put("pkg", pkg);
        map.put("versionCode", versionCode);
        map.put("page", page);

        return gson.toJson(map);
    }


    /**
     * 评论打分
     */
    public static String toCommentJson(String pkg, int versionCode, float score, String comment) {
        baseInfo = JWTApp.getInstance().getBaseInfo();
        baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.COMMENT);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("baseInfo", baseInfo);
        map.put("pkg", pkg);
        map.put("versionCode", versionCode);
        map.put("score", score);
        map.put("comment", comment);

        return gson.toJson(map);
    }

}

